<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Bar extends Model
{
    use SoftDeletes;

    protected $table = 'bar';

    protected $guarded = [];


    public function posts(){

        return $this->hasMany(Post::class,'bar_num','bar_num');

    }


//    public function can



    public function author(){

        return $this->belongsTo(User::class,'uid','id');


    }

    // 一个贴吧可以有多个粉丝

    public function fans(){

        return $this->hasMany(BarFans::class,'bar_num','bar_num');
    }


    protected static function boot()
    {
        parent::boot();

        static::updating(function ($bar){

            $cover = $bar->bar_cover;

            if(! preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$cover)){


                if (substr($cover,0,4) == 'head'){
                    $cover = substr($cover,5);

                }

                $bar->bar_cover = url('api/user/output/'.$cover);

            }

        });

    }
}
